package eerbaar.client.internal.ctr;

import eerbaar.client.ctr.IGameController;
import eerbaar.client.ctr.IPersonaCmd;
import eerbaar.game.IGame;
import eerbaar.persona.IPersona;
import eerbaar.position.WrongPositionException;

public class MultiGameController implements IGameController {

    private final IGame game;

    private final IPersona[] subjects;

    public MultiGameController(IPersona[] subjects, IGame game) {
        this.game = game;
        this.subjects = subjects;
    }

    @Override
    public boolean input(String input) {
        assert (input != null);

        return process(input);
    }

    private boolean process(String input) {
        assert (input != null);

        boolean result = true;

        IPersona[] personas = subjects;

        IMultiStimuliHandler handler = StimuliHandlerFactory.get();

        if (handler == null) {
            return false;
        }

        for (IPersona persona : personas) {

            IPersonaCmd cmd = handler.command(input);
            if (cmd != null) {
                try {
                    result = cmd.execute(this.game, persona);
                } catch (WrongPositionException e) {
                    // ignore
                }
            }
        }

        return result;
    }

}
